using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class PhantomWall : MonoBehaviour
{
    private Tilemap _TM;
    private bool _isRevealed;
    public int _disappearTimeSetting;
    private int _disappearRemain;
    public int DisappearRemain { get => _disappearRemain; set => _disappearRemain = Math.Min(Math.Max(value, 0), _disappearTimeSetting); }

    void Start()
    {
        _TM = GetComponent<Tilemap>();
        DisappearRemain = _disappearTimeSetting;
        _isRevealed = false;
    }

    void Update()
    {
        Disappear();
    }

    private void Disappear()
    {
        if (_isRevealed) DisappearRemain--;
        else DisappearRemain++;
        Color color = _TM.color;
        color.a = (float)DisappearRemain / (float)_disappearTimeSetting;
        _TM.color = color;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.transform.tag == "Player") _isRevealed = true;
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.transform.tag == "Player") _isRevealed = false;
    }
}
